#include "Protocols/PPPOEProtocal.h"
#include "utilities/ReadablePacket.h"
#include "protocols/ProtocalCode.h"
#include "protocols/ProtocalPool.h"
#include "protocols/ShowableItem.h"
#include "utilities/utilities.h"

REGISTER_PROTOCOL(PPPOEProtocal)

void PPPOEHeader::toShowableItems(ShowableItem* root) const
{
	HeaderFormat* header = (HeaderFormat*) mData;
	int pos = mPos;
	ShowableItem *temp;

	ShowableItem* proto = createProtocalItem("PPP-over-Ethernet", pos, getHeaderLength());
	root->pushChild(proto);

	int num = header->_mVer_Type & 0xF0;
	temp = createItem("Version:", num, pos, 0);
	proto->pushChild(temp);

	num = header->_mVer_Type & 0x0F;
	temp = createItem("Type:", num, pos, 1);
	proto->pushChild(temp);

	temp = createItem("Code: ",toHexString(pointer(pos), 1), pos, 1);
	proto->pushChild(temp);

	temp = createItem("Session ID: ", toHexString(pointer(pos), 2), pos, 2);
	proto->pushChild(temp);

	temp = createItem("Payload Length: ", header->_mLen, pos, 2);
	proto->pushChild(temp);
}

PPPOEProtocal::PPPOEProtocal():Protocol(PPPOE, QString("PPPOE"))
{
}

PacketHeader*
PPPOEProtocal::analyzeProc(ReadablePacket* packet,int low,int high,Protocol*& tip)
{
	PPPOEHeader* header = new PPPOEHeader(PROTOCOL_POINTER(PPPOEProtocal), packet->getData(), low, high - low + 1);

	static Protocol* ppp_prop = ProtocolPool::getProtocal(PPP_PPPOE).get();

	tip = ppp_prop;
// 	if (header->getTypeCode())
// 	{
// 		tip = header->getTypeCode();
// 	}
// 	else
// 	{
// 		tip.reset();
// 	}
	
	return header;
}